package com.ifengchao.util.sina;

import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ThridPartyFuncionUtil {
	private static final Log logger = LogFactory
			.getLog(ThridPartyFuncionUtil.class);
	
	
	public static Map<String,String> queryBalance(Map<String, String> paramMap){
		String url = ThridPartyEnvUtil.getValue("queryBalance.url");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		

		logger.debug("sina查询余额请求参数："+requestParam);

		String queryBalanceResult = HttpUtil.sendPostByHttp(url, requestParam);
		
		if( null == queryBalanceResult ){
			logger.error("sina查询余额失败");
			throw new RuntimeException("sina查询余额失败");
		}else{
			//解析发送结果
			Map<String,String> queryBalanceResultMap = JsonUtil.getMapFromJsonString(queryBalanceResult);
			if(null == queryBalanceResultMap  || "sysException".equals(queryBalanceResultMap.get("rescode"))){
				// 系统异常
				logger.error("sina查询余额失败:" + String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
				throw new RuntimeException("sina查询余额失败");
			}else if("BusinessException".equals(queryBalanceResultMap.get("rescode"))){
				// 业务异常，把错误信息提示给用户
				logger.error("sina查询余额失败:" + String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
				throw new RuntimeException(String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
			}
			return queryBalanceResultMap;
		}
		
	}
	
	public static Map<String,String> queryAccountDetails(Map<String, String> paramMap){
		String url = ThridPartyEnvUtil.getValue("queryAccountDetails.url");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}

		logger.debug("sina查询收支明细请求参数："+requestParam);

		String queryBalanceResult = HttpUtil.sendPostByHttp(url, requestParam);
		System.out.println(queryBalanceResult);
		if( null == queryBalanceResult ){
			logger.error("sina查询收支明失败");
			throw new RuntimeException("sina查询收支明失败");
		}else{
			//解析发送结果
			Map<String,String> queryBalanceResultMap = JsonUtil.getMapFromJsonString(queryBalanceResult);
			if(null == queryBalanceResultMap  || "sysException".equals(queryBalanceResultMap.get("rescode"))){
				// 系统异常
				logger.error("sina查询收支明失败:" + String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
				throw new RuntimeException("sina查询收支明失败");
			}else if("BusinessException".equals(queryBalanceResultMap.get("rescode"))){
				// 业务异常，把错误信息提示给用户
				logger.error("sina查询收支明失败:" + String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
				throw new RuntimeException(String.valueOf(queryBalanceResultMap.get("resmsg_cn")));
			}
			return queryBalanceResultMap;
		}
	}
}
